#-*- coding: utf-8 -*-

#--- FRAMEWORK IMPORTS ---
from django import forms
from django.contrib import admin
from django.utils.translation import ugettext as _

#--- APP IMPORTS ---
from forum.models import Topic, Reply, Poll, PollOption
from forum.models import Forum, ForumGroup, ForumProfile 
from forum.models import View

from forum.forms import ForumFormAdmin, TopicFormAdmin, PollOptionFormAdmin


class ForumAdmin(admin.ModelAdmin):    
    form = ForumFormAdmin

    fieldsets = (
        (None, {
            'fields':('active', 'group', 'parent',
                     'title', 'slug', 'short', 'moderation')
            }),
        (_('Graphics'), {
            'classes':('collapse',),
            'fields':('image', 'image_small')
            }),
        (_('Advanced Options'), {
            'classes':('collapse',),
            'fields': ('allow_view_only_to_logged_users',
                       'allow_topics', 'allow_reply_edit', 'allow_reply_del', #'allow_polls', 'allow_rating', 'hide_new_messages'
                       'post_notify')
            }))
    
    class Media: js = ("js/mootools-1.2.4-core-yc.js", "js/forum_lib.js")

    
    def change_view(self, request, object_id, extra_context=None):
        return super(ForumAdmin, self).change_view(request, object_id,
            extra_context)
    
    def save_model(self, request, obj, form, change):
        if not change:
            obj.created_by = request.user
        obj.save()


class ForumGroupAdmin(admin.ModelAdmin):
    def save_model(self, request, obj, form, change):
        if not change:
            obj.created_by = request.user
        obj.save()
    exclude = ('created_by',)


class TopicAdmin(admin.ModelAdmin):
    form = TopicFormAdmin

    class Media:
        class Media: js = ("js/mootools-1.2.4-core-yc.js", "js/forum_lib.js")


class PollOptionAdmin(admin.ModelAdmin):
    form = PollOptionFormAdmin

class PollOptionInline(admin.TabularInline):
    model = PollOption
    form = PollOptionFormAdmin
    extra = 4

class PollAdmin(admin.ModelAdmin):
    inlines = [
        PollOptionInline
        ]

#--- CADASTROS ESPECIAIS ---
admin.site.register(Poll, PollAdmin)
admin.site.register(Topic, TopicAdmin)
admin.site.register(Forum, ForumAdmin)
admin.site.register(ForumGroup, ForumGroupAdmin)

#--- CADASTROS DIRETOS ---
admin.site.register(ForumProfile)
admin.site.register(View)
admin.site.register(Reply)
admin.site.register(PollOption, PollOptionAdmin)
